/*  JEVEMon, A Java-based EVEMon clone.
Copyright (C) 2011, Raul Ruiz

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jeveme.datacontainers;

import eveapi.EVEApiConnector;
import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author ruizr
 */
public class EVEAccount implements Serializable{
    /**
     * 
     */
    private String m_userID;
    
    /**
     * 
     */
    private String m_apiKey;
    
    /**
     * 
     */
    private CredentialsLevel m_credentialsLevel;
    
    /**
     * An array of characters associated with this account.
     */
    private ArrayList<EVECharacter> m_charactersOnAccount;
    
    /**
     * 
     * @param userID
     * @param apiKey 
     */
    public EVEAccount(String userID, String apiKey) {
        setUserID(userID);
        setApiKey(apiKey);
        setCredentialsLevel(CredentialsLevel.Unknown);
        m_charactersOnAccount = new ArrayList<EVECharacter>();
    }
    
    /**
     * Fetches data from CCP to determine whether this is a limited or full API Key. This
     * does not simply return the variable m_credentialsLevel, but actually makes
     * a call to the CCP/EVE API.
     * @param userID
     * @param apiKey
     * @return 
     */
    public static CredentialsLevel getAccountType(String userID, String apiKey) {
        String xml_response = EVEApiConnector.getAccountStatus(userID, apiKey);
        if(xml_response.contains("<error code=\"200\">")) {
            return CredentialsLevel.Limited;
        }
        else {
            return CredentialsLevel.Full;
        }
        
    }
    
    /**
     * Adds a character associated with this account.
     * @param ec An EVECharacter object to be associated with this account.
     */
    public void addCharacter(EVECharacter ec) {
        m_charactersOnAccount.add(ec);
    }
    
    public ArrayList<EVECharacter> getCharacters() {
        return m_charactersOnAccount;
    }
    
    /**
     * Finds a character on an account given the character's name.
     * @param characterName
     * @return 
     */
    public EVECharacter getCharacterByName(String characterName) {
        EVECharacter ec = new EVECharacter(this);
        for(int i=0; i < m_charactersOnAccount.size(); i++) {
            if(m_charactersOnAccount.get(i).getCharacterName().equals(characterName)) {
                ec = m_charactersOnAccount.get(i);
            }
        }
        
        return ec;
    }
    
    /**
     * 
     * @return 
     */
    public final String getUserID() {
        return m_userID;
    }
    
    /**
     * 
     * @param id 
     */
    public final void setUserID(String id) {
        m_userID = id;
    }
    
    /**
     * 
     * @return 
     */
    public final String getApiKey() {
        return m_apiKey;
    }
    
    /**
     * 
     * @param key 
     */
    public final void setApiKey(String key) {
        m_apiKey = key;
    }
    
    /**
     * 
     * @return 
     */
    public final CredentialsLevel getCredentialsLevel() {
        return m_credentialsLevel;
    }
    
    /**
     * 
     * @param cl 
     */
    public final void setCredentialsLevel(CredentialsLevel cl) {
        m_credentialsLevel = cl;
    }
}
